package ael.imarkket.Library;

import ael.imarkket.Login;
import android.content.Context;
import android.content.SharedPreferences;

public class Preferencing {

	private static final String			APP_PREFERENCIA		= "APP_CONFIG";
	private SharedPreferences			appSharedPrefs;
	private SharedPreferences.Editor	prefsEditor;
	private String						usernamePref		= "username";
	private String						passwordPref		= "password";
	private String						checkRemenberPref	= "checkRemenberPref";
	private String						checkAutoLoginPref	= "checkAutoLoginPref";

	private String						usernamePrefTemp	= "username_temp";
	private String						passwordPrefTemp	= "password_temp";

	public Preferencing(Context context) {
		this.appSharedPrefs = context.getSharedPreferences(APP_PREFERENCIA, Login.MODE_PRIVATE);
		this.prefsEditor = appSharedPrefs.edit();
	}

	public void setUsername(String username) {
		prefsEditor.putString(usernamePref, username).commit();
	}

	public void setPassword(String password) {
		prefsEditor.putString(passwordPref, password).commit();
	}

	public void setUsernameTemp(String username) {
		prefsEditor.putString(usernamePrefTemp, username).commit();
	}

	public void setPasswordTemp(String password) {
		prefsEditor.putString(passwordPrefTemp, password).commit();
	}

	public Boolean getCheckRemenberPref() {
		return appSharedPrefs.getBoolean(checkRemenberPref, false);
	}

	public void setCheckRemenberPref(Boolean checkRemenber) {
		prefsEditor.putBoolean(checkRemenberPref, checkRemenber).commit();
	}

	public String getUsername(Boolean isLoadingApp) {
		if ((this.getCheckRemenberPref()) || (isLoadingApp)) {
			return appSharedPrefs.getString(usernamePref, "default@app.com");
		} else {
			return appSharedPrefs.getString(usernamePrefTemp, "temporal@app.com");
		}
	}

	public String getPassword(Boolean isLoadingApp) {
		// isLoadinApp se utiliza para poner los valores por defecto si está entrando a la app o si está el check activdo
		if ((this.getCheckRemenberPref()) || (isLoadingApp)) {
			return appSharedPrefs.getString(passwordPref, "default");
		} else {
			return appSharedPrefs.getString(passwordPrefTemp, "temporal");
		}
	}

	public String getPasswordTemp() {
		return appSharedPrefs.getString(passwordPrefTemp, "temporal");
	}

	public String getUsernameTemp() {
		return appSharedPrefs.getString(usernamePrefTemp, "temporal@app.com");
	}

	public Boolean getAutoLogin() {
		return appSharedPrefs.getBoolean(checkAutoLoginPref, false);
	}

	public void setAutoLogin(Boolean checkAutoLogin) {
		prefsEditor.putBoolean(checkAutoLoginPref, checkAutoLogin).commit();
	}

}
